package com.quasar.hpatient.Jpush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.event.UpdateEvent;
import com.quasar.hpatient.model.litePalmodel.JPushDataDatadb;
import com.quasar.hpatient.model.medicalmodel.PatientInfoData;
import com.quasar.hpatient.utils.MsgJumpView;
import com.quasar.hpatient.view.prescription.AtPrescriptionActivity_;
import com.quasar.hpatient.view.record.TestRecordActivity_;
import com.vise.log.ViseLog;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.jpush.android.api.JPushInterface;

/**
 * name : lishuai
 * time : 2017/11/10
 * title:
 */

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "JPush";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    PatientInfoData doctorinfo = MainAppliaction.getInstance().getPatient(); //获取医生信息


    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
//        Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

        //推送的内容
        String extras = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        //推送的标识
        String funCode = bundle.getString(JPushInterface.EXTRA_EXTRA);
        ViseLog.d("打开的通知"+extras+"****"+funCode);
        JpushBean jpushBean= JSON.parseObject(funCode,JpushBean.class);

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            processCustomMessage(context, bundle);
            ViseLog.d("推送过来的值"+funCode);

            JPushDataDatadb jPushDataDataDB = new JPushDataDatadb(
                    1,
                    extras,
                    funCode,
                    sdf.format(new Date()), doctorinfo.getBmobUserName());
            jPushDataDataDB.save();
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            //接收到推送消息
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
            UpdateEvent updateEvent = new UpdateEvent();
            if(jpushBean.getFunCode()!=null){
                if(jpushBean.getFunCode().equals("1") || jpushBean.getFunCode().equals("2")){//医生申请信息
                    updateEvent.type = ConstantManger.MYDOCTORX;//
                    EventBus.getDefault().post(updateEvent);
                }else if(jpushBean.getFunCode().equals("8")){//诊断信息
                    updateEvent.type = ConstantManger.ClosePhoto;
                    EventBus.getDefault().post(updateEvent);
                }else if(jpushBean.getFunCode().equals("9") || jpushBean.getFunCode().equals("10") ||
                        jpushBean.getFunCode().equals("11") || jpushBean.getFunCode().equals("12") || jpushBean.getFunCode().equals("13")){//调药信息
                    updateEvent.type = ConstantManger.MYGUESS;
                    EventBus.getDefault().post(updateEvent);
                }
            }
            updateEvent.type = ConstantManger.JPSHMAG;
            EventBus.getDefault().post(updateEvent);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
            //跳转到化验单界面
            MsgJumpView.JumpView(context,jpushBean.getFunCode(),jpushBean.getTime());

            ViseLog.d("打开的通知消息"+jpushBean.getTime());
            UpdateEvent updateEvent = new UpdateEvent();
            updateEvent.type = ConstantManger.JPSHMAG;
            EventBus.getDefault().post(updateEvent);

//            if(jpushBean.getFunCode().equals("2") || jpushBean.getFunCode().equals("3")){
//                UpdateEvent updateDoctorx = new UpdateEvent();
//                updateEvent.type = ConstantManger.MYDOCTORX;
//                EventBus.getDefault().post(updateDoctorx);
//            }
        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..

        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            Log.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
        } else {
            Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
        }
    }

    // 打印所有的 intent extra 数据
    private static void printBundle(Bundle bundle) {
//        ...//省略了

    }

    //send msg to MainActivity
    private void processCustomMessage(Context context, Bundle bundle) {
        //  ...//省略了
    }
}